package tsing.good_simple.lock_impl;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Date 2024/11/25 13:57
 * @Author Tsing
 *
 * 单体式应用普通锁
 */
public class SingleLockImpl {



    private final ReentrantLock lock = new ReentrantLock();


    /**
     * 一般加锁的方式
     * @throws InterruptedException
     */
    public void syncMethod1() throws InterruptedException {

        boolean lockFlag = lock.tryLock(1, TimeUnit.MILLISECONDS);
        if(!lockFlag){
            return ;
        }
        try {
            // 加锁成功,执行业务方法
            coreMethod();

        } finally {
            // 解锁
            lock.unlock();
        }
    }


    /**
     * 模拟业务方法
     */
    public void coreMethod(){

        System.out.println("do something");
    }

}
